﻿@model Sealight.Core.DataAdapter.product_category
@{
    ViewBag.Title = "Thêm sửa loại sản phẩm";
    Layout = "~/Areas/Admin/Views/Shared/__AdminLayout.cshtml";
}
@section _Head{
    <script type="text/javascript">
        $(function () {
            $("#category_tabs").tabs();
        });
        $(document).ready(function () {



            var upload = new AjaxUpload('#file_field', {
                action: '/file/UploadFile',
                data: { 'folder': 'pictures' },
                responseType: 'true',
                onComplete: function (file, response) {
                    if (response != '0') {
                        $("#image_url").val(response);
                        var img_src = "/Content/images/pictures/" + response;
                        var img_url = GetImageUrl(img_src, 100, 111, false, 100);
                        $("#picture_field_image").attr("src", img_url);
                    }
                    else {

                        ShowErrorMessage('invalid file extension (jpg|png|jpeg|gif).');
                    }
                }
            });

            var container = $('div.container_error');
            $("#data_form").validate({
                rules: {
                    name_field: {// compound rule 
                        required: true
                    }
                },
                messages: {
                    name_field: {
                        required: "Vui lòng nhập tên."
                    }
                },
                errorElement: "li",
                errorContainer: container,
                errorLabelContainer: $(".error", container)
            ,
                submitHandler: function (form) {
                    var data = {
                        id: $("#category_id").val(),
                        name: $("#name_field").val(),
                        description: $("#description_field").val(),
                        parent_id: $("#category_field").val(),
                        meta_keywords: $("#meta_keywords_field").val(),
                        meta_description: $("#meta_description_fields").val(),
                        meta_title: $("#meta_title_field").val(),
                        se_name: $("#seo_title_field").val(),
                        page_size: $("#page_size_field").val(),
                        price_ranges: $("#price_range_field").val(),
                        published: $("#publish_field").attr('checked'),
                        display_order: $("#order_field").val(),
                        picture: {
                            id: $("#image_id").val(),
                            picture_url: $("#image_url").val()
                        }
                    };

                    JsUtils.ShowLoader('#data_form', 'Lưu...');
                    $.ajax({
                        type: "POST",
                        url: "/admin/product/_SaveCategoryData",
                        data: JSON.stringify(data),
                        traditional: true,
                        dataType: "json",
                        contentType: 'application/json',
                        error: function (xhr, status, error) { },
                        success: function (d) {
                            if (d != null) {
                                if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                                    if (d.ErrorFriendlyMessage != null) {
                                        ShowErrorMessage(d.ErrorFriendlyMessage);
                                    }
                                    return;
                                }
                                else {
                                    ShowErrorMessage("Lưu thành công", "thông báo");
                                    window.location = "/admin/product/ManageCategoryMain";
                                }
                            }
                        },
                        complete: function (d) {
                            JsUtils.hideLoader('#data_form');
                        },
                        cache: false
                    }); //end .ajax call
                }
            }); //end form.validate       
        });
    </script>
}
<h2>
    Thêm mới loại sản phẩm</h2>
<div>
    <form id="data_form" method="post" action="" enctype="multipart/form-data">
    <div class="options">
    <input type="submit"   value="Lưu" id="btnAddNew" class="adminButtonBlue" title="Lưu"/>
    </div>
    <div class="container_error">
        <h5>
            Vui lòng kiểm tra lại các lỗi...</h5>
        <ol class="error">
        </ol>
    </div>
    <div id="category_tabs">
        <ul>
            <li><a href="#tabs-1">Thông tin loại</a></li>
            <li><a href="#tabs-2">SEO</a></li>           
        </ul>
        <div id="tabs-1">
            @Html.Partial("~/Areas/Admin/Views/Product/_AddUpdateCategoryInformation.cshtml", Model)
        </div>
        <div id="tabs-2">
            @Html.Partial("~/Areas/Admin/Views/Product/_AddCategorySEO.cshtml", Model)
        </div>     
    </div>
    </form>
</div>
